#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>

/*
void* func(void* agr)
{
	printf("线程创建成功\n");
	printf("pid = %d, tid = %lu\n",getpid(), pthread_self());
}

int main()
{

	pthread_t tid;
	int ret;
	//创建线程
	ret = pthread_create(&tid, NULL, func, NULL);
	printf("tid = %lu\n", tid);
	while(1)
	{
		sleep(1);
	}
	return 0;

}
*/

//======================================

/*//线程回收
void* func(void* agr)
{
	printf("agr = %d\n",(int)agr);
	printf("线程创建成功, tid = %lu\n",pthread_self());
	sleep(10);
	pthread_exit("线程已被回收");

}



int main()
{
	pthread_t tid;
	int i = 5;
	void* retv;
	int ret;
	ret = pthread_create(&tid, NULL, func, (void*)i);
	sleep(1);
	pthread_join(tid, &retv);
	printf("retv = %s\n", (char*)retv);
	return 0;
}
*/


//===========================================

/*void* func(void* agr)
{

	printf("pid= %d：线程创建成功tid =%lu\n",getpid(), pthread_self());
	//取消点
	while(1)
	{
		sleep(1);
	}
	pthread_exit("线程 ,已被回收");
	
}

int main()
{
	int ret;
	int i;
	void* retv;
	pthread_t tid[100];
	//线程创建
	for(i = 0; i < 100; i++)
	{
		ret = pthread_create(&tid[i], NULL, func, NULL);
	}
	//线程回收
	for(i = 0; i < 100; i++)
	{
		pthread_join(tid[i], &retv);
		printf("%d:%s\n",i+1,(char*)retv);
	}
	while(1)
	{
		sleep(1);
	}
}
*/

//=======================================

/*//1.线程属性分离
void* fun(void* agr)
{
	//设定分离
	pthread_detach(pthread_self());
	printf("pid = %d, tid = %lu",getpid(),pthread_self());
	printf("线程创建成功\n");

	//取消点
	sleep(5);
	
	pthread_exit("已被回收");
}


int main()
{
	pthread_t tid;
	int ret;
	//创建线程
	ret = pthread_create(&tid, NULL, fun, NULL);
	sleep(10);
	//也可以在这里设
	//pthread_detach(tid);
	return 0;
}
*/

//================================

/*int main()
{
	pid_t pid;
	
	//创建进程
	pid = fork();
	if(pid < 0)
	{
		perror("fork");
		return 0;
	}else if(pid == 0)
	{
		exit(0);
	}else
	{
		printf("子进程创建成功\n");
		printf("pid = %d, ppid = %d\n", getpid(),getppid());
		sleep(15);
	}
}*/


//===============================

//属性分离

void* func(void* agr)
{
	printf("线程创建成功\n");
	
	//取消点
	while(1)
	{
		sleep(1);
	}

}



int main()
{
	pthread_t tid;
	int ret;
	
	//设在属性
	pthread_attr_t attr;
	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

	//创建线程
	ret = pthread_create(&tid, &attr, func, NULL);
	while(1)
	{
		sleep(1);
	}
	return 0;
}












































































